import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:xiongmao_clean_flutter_module/business/common/todo/page/todo_page.dart';

import '../business/common/approve/approve_router.dart';
import '../business/common/contract/contract_ history_router.dart';
import '../business/common/contract/contract_router.dart';
import '../business/common/credit_inquiry/credit_router.dart';
import '../business/common/identity/identity_router.dart';
import '../business/common/insure/Insure_router.dart';
import '../business/common/notfound/not_found_page.dart';
import '../business/common/todo/todo_router.dart';
import '../business/common/web/web_router.dart';
import '../business/common/workpost/work_post_router.dart';

const _notFoundPage = "notFoundPage";

/// 根据页面名称定义对应的页面对象
Map<String, FlutterBoostRouteFactory> _routerMap = {
  _notFoundPage: (settings, uniqueId) {
    return MaterialPageRoute(
        settings: settings,
        builder: (_) {
          return const NotFoundPage();
        });
  },
};

void initRouters() {
  _routerMap.addAll(webRouterMap); //webView
  _routerMap.addAll(todoRouterMap); //todo审批流 列表
  _routerMap.addAll(identityRouterMap); //身份核验
  _routerMap.addAll(creditInquiryRouterMap); //信用查询
  _routerMap.addAll(contractRouterMap); //电子合同
  _routerMap.addAll(contractPayRouterMap); //电子合同
  _routerMap.addAll(insureRouterMap); //保险
  _routerMap.addAll(workPostRouterMap); //岗位管理
  _routerMap.addAll(workAddPostRouterMap); //岗位管理
  _routerMap.addAll(workRulesRouterMap); //用工规则
  _routerMap.addAll(appRoveRouterMap); //审批规则入口
  _routerMap.addAll(appEntryDepartRouterMap); //编辑审批规则设置
  _routerMap.addAll(appSettingsRouterMap); //审批规则设置
  _routerMap.addAll(contractPaySuccessMap); //电子合同购买成功
  _routerMap.addAll(contractHistoryMap); //电子历史页面
  _routerMap.addAll(contractAutoSignPageMap); //电子历史页面
  _routerMap.addAll(contractEditSealPageMap); //电子历史页面
  _routerMap.addAll(addContractTemplatePageMap); //添加合同模版
  _routerMap.addAll(contractTemplateReviewPageMap); //合同模版预览
}

/// FlutterBoost 创建路由数据时使用
Route<dynamic>? routeFactory(RouteSettings settings, String? uniqueId) {
  print("flutter settings name =  ${settings.name}");
  // 跳转到native页面
  if (settings.name != null && settings.name!.startsWith("native_")) {
    print("flutter routeFactory =  return null");
    return null;
  }
  // 否则跳转到flutter页面
  var pageName = settings.name;
  if (pageName == '/') {
    // 初始化，空页面
    pageName = contractPage;

    ///切记：在上线时改为_notFoundPage，只用flutter测试时可用其他页面
    // pageName = _notFoundPage;
  }
  FlutterBoostRouteFactory func = _routerMap[pageName] as FlutterBoostRouteFactory;
  print("flutter routeFactory fuc =  $func");
  return func(settings, uniqueId);
}

/// FlutterBoost 创建路由数据时使用
Widget appBuilder(Widget home) {
  return MaterialApp(
    home: home,
    debugShowCheckedModeBanner: true,

    ///必须加上builder参数，否则showDialog等会出问题
    builder: (_, __) {
      return home;
    },
  );
}
